<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码11" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in ld" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
// import { getEmployeeSimple } from '../../../api/employess'
import { getEmployeeSimple, getDepartDetail, updateDepartments } from '../../../api/employess'
export default {
  props: {
    bianjiid: {
      type: String,
      required: true
    },
    nocodecf: {
      type: Array,
      required: true
    },
    jisuanbj: {
      type: Array,
      required: true
    }
  },
  data() {
    // const validCode = (rule, value, callback) => {
    //   // console.log(this.nocodecf, value)
    //   this.nocodecf.includes(value)
    //     ? callback(`部门编码 ${value}  已经占用`) : callback()
    // }
    const validName = (rule, value, callback) => {
      const list = this.jisuanbj.filter(item => item !== this._name)
      // console.log(list, value)
      list.includes(value) ? callback(`部门名字 ${value}  已经占用`) : callback()
    }
    return {

      ld: [],
      form: {
        name: '', // 部门名称 w
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },

      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 2, max: 10, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validName, trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] }
          // { validator: validCode, trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  mounted() {
    this.getEmployeeSimple()
    this.getDepartDetail()
  },
  methods: {
    async getEmployeeSimple() {
      const re = await getEmployeeSimple()
      // console.log(re.data)
      // this.quanbuld=re.
      // console.log(re)
      // console.log(re)
      this.ld = re.data
      // console.log(this.ld)
    },
    async getDepartDetail() {
      const re = await getDepartDetail(this.bianjiid)

      // 根据id找到编辑的内容重写到编辑页面上
      this.form = re.data
      this._code = re.data.code
      this._name = re.data.name
      // console.log(re)
    },
    async updateDepartments() {
      // const list = { ...this.form, pid: this.bianjiid }
      await updateDepartments(this.form)
      // console.log(re)
    },
    hSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          console.log('submit!')
          this.updateDepartments()
          this.$emit('genxin')
          this.$emit('closee')
          // 通过
        } else {
          this.$message({
            showClose: true,
            message: '请按要求填写完毕',
            type: 'error'
          })
        }
      })

    //   console.log('提交')
    },
    hCancel() {
    //   console.log('取消')
      this.$emit('abc')
    }
  }

}
</script>

<style>

</style>
